import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { globalConfig, $fn, $http, $Lazy } = window
const { COVIDUrl } = globalConfig
// ===================================================================== 
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data: [],
		// 客户变更
		formData:[
			{ label: '开始条码', 	name:'start_code',	half:true},
			{ label: '结束条码', 	name:'end_code',	half:true,	
				btn:{ label:'生成',	 onClick:value=>{this.generateCode()} }},
			{ label: '条码号', 		name:'spec_code', 	full:true, 		type:'textarea',		required:true },
			{ label: '原医院', 		name:'old_hosp_id', 	full:true, 		type:'select',		required:true,	idStr:'uuid',	nameStr:'hospName'},
			{ label: '新医院', 		name:'new_hosp_id', 	full:true, 		type:'select',		required:true,	idStr:'uuid',	nameStr:'hospName'},
		]
	}
	
	componentDidMount(){
		this.getSelect()
	}
	open = ()=> {
		this.modal.open({formRef: this.formRef})
	}
	
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			// 医院
			this.hostData = await f.getCache({ api:COVIDUrl+'Hospital/query',param:{page:1,page_num:10000,status:1} })
			$fn.setRow( formData, 'old_hosp_id',  this.hostData)
			$fn.setRow( formData, 'new_hosp_id',  this.hostData)
			//
			this.setState({ formData })
		})
	}
	
	// 生成条码
	generateCode = () => {
		const {start_code, end_code} = this.formRef.getValue(['start_code','end_code'])
		if(start_code&&end_code&&start_code.length>=8&&start_code.length<=32&&end_code.length>=8&&end_code.length<=32){
			if(+start_code > +end_code ){
				return $fn.msg.warning('开始条码必须小于结束条码')
			}else{
				const arr =[]
				for(var i = parseInt(start_code); i <= parseInt(end_code); i++){
					arr.push(i)
				}
				let {spec_code} = this.formRef.getValue(['spec_code'])					
				let specData = []
				if(spec_code){
					spec_code = spec_code.match(/\d{8,32}/g)
					spec_code.forEach(v=>{
						specData.push(parseInt(v))
					})
				}
				spec_code = Array.from(new Set([...specData, ...arr]))
				this.formRef.setValue({spec_code: spec_code.join('\r')})
			}
		}else{
			return  $fn.msg.warning('开始条码或结束不正确')
		}
	}
	
	render(){
		return FormModal.call(this,{ title:'客户变更', labelWidth:'s5', width:800,enter:false,
			onSubmit:() => {
				const { spec_code, old_hosp_id, new_hosp_id } = this.formRef.getValue()
				if(spec_code){
					let specStr = spec_code.match(/\d{8,32}/g)
					specStr = Array.from(new Set(specStr)).join(',')
					$http.submit(this,COVIDUrl+'caseinfo/chhospital',{ param:{spec_code:specStr, old_hosp_id, new_hosp_id}}).then(data=>{
						this.modal.close()
						this.props.onRefresh()
					})
				}
			}
		})
	}
}

export default Index